package com.topone.controller;

import com.topone.Converter.ShopForm2shopApply;
import com.topone.Enum.ResultEnum;
import com.topone.Enum.ShopApplyStatusEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.ShopApplyForm;
import com.topone.Utils.KeyUntil;
import com.topone.dataObject.ShopApply;
import com.topone.service.ShopApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/23,0023.
 */
@Controller
@RequestMapping("/seller/shopApply")
@Slf4j
public class ShopApplyController {

    @Autowired
    private ShopApplyService shopApplyService;

    @GetMapping("/create")
    public ModelAndView index(@RequestParam(value = "shopId" ,defaultValue = "") String shopId,
                              Map<String,Object> map){
        ShopApply shopApply=new ShopApply();
        try {
            shopApply=shopApplyService.findOne(shopId);

        }catch (FanZoneException e){
            log.error("【查询商家申请】查询异常",e);
            map.put("msg",e.getMessage());
            map.put("url","/fanZone/seller/shopApply/list");
            return  new ModelAndView("common/error",map);
        }
        map.put("shopApply",shopApply);
        return new ModelAndView("shopApply/create",map);
    }

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map){

        PageRequest request=new PageRequest(page-1,size);
        Page<ShopApply> shopApplyPage=shopApplyService.findAll(request);
        map.put("size",size);
        map.put("currentPage",page);
        map.put("shopApplyPage",shopApplyPage);
        return new ModelAndView("/shopApply/list",map);
    }
    @GetMapping("/check")
    public ModelAndView check(Map<String,Object> map,
                              @RequestParam("shopId") String shopId){
        try {
            ShopApply shopApply = shopApplyService.findOne(shopId);
            if(ShopApplyStatusEnum.WAIT.getCode()!=shopApply.getApplyStatus()){
                log.error("【商家申请入驻处理】申请状态不正确,status={}",shopApply.getApplyStatus());
                map.put("msg", "商家申请状态不正确");
                map.put("url", "/fanZone/seller/shopApply/list");
                return new ModelAndView("common/error", map);
            }
            shopApply.setApplyStatus(ShopApplyStatusEnum.SUCCESS.getCode());
            shopApplyService.save(shopApply);
        }catch (FanZoneException e) {
            log.error("【商家申请入驻处理】异常,e={}",e.getMessage());
            map.put("msg", e.getMessage());
            map.put("url", "/fanZone/seller/shopApply/list");
            return new ModelAndView("common/error", map);
        }
        log.info("【商家申请入驻处理】 入驻成功 shopId={}",shopId);
        map.put("orderId",shopId);
        map.put("msg", ResultEnum.SHOPAPPLY_CHACK_SUCCESS.getMessage());
        map.put("url", "/fanZone/seller/shopApply/list");
        return new ModelAndView("/common/success",map);
    }

    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("shopId") String shopId,
                               Map<String,Object> map){
        try {
            ShopApply shopApply = shopApplyService.findOne(shopId);
            if(ShopApplyStatusEnum.WAIT.getCode()!=shopApply.getApplyStatus()){
                log.error("【商家申请取消处理】申请状态不正确,status={}",shopApply.getApplyStatus());
                map.put("msg", "商家申请取消状态不正确");
                map.put("url", "/fanZone/seller/shopApply/list");
                return new ModelAndView("common/error", map);
            }
            shopApply.setApplyStatus(ShopApplyStatusEnum.fail.getCode());
            shopApplyService.save(shopApply);
        }catch (FanZoneException e) {
            log.error("【商家入驻取消处理】异常,e={}",e.getMessage());
            map.put("msg", e.getMessage());
            map.put("url", "/fanZone/seller/shopApply/list");
            return new ModelAndView("common/error", map);
        }
        log.info("【商家入驻取消处理】 取消成功 shopId={}",shopId);
        map.put("orderId",shopId);
        map.put("msg", ResultEnum.SHOPAPPLY_CANCAL_SUCCESS.getMessage());
        map.put("url", "/fanZone/seller/shopApply/list");
        return new ModelAndView("/common/success", map);
    }

   @GetMapping("/index")
   public ModelAndView index(){
        return new ModelAndView("/buyer/personal_info");
   }

    @PostMapping("/save")
    public ModelAndView frontSave(@Valid ShopApplyForm shopApplyForm,
                                                  BindingResult bindingResult,
                                  Map<String,Object> map){
        if(bindingResult.hasErrors()){
            log.error("【商家申请入职】参数不正确,bussinessForm={}",shopApplyForm);
            map.put("error","提交参数不正确");
            return new ModelAndView("/common/error",map);
        }

        ShopApply shopApply= ShopForm2shopApply.convert(shopApplyForm);

        //生成shopID，也是bussinessId
        shopApply.setShopId(KeyUntil.getUniqueKey());
        ShopApply result=new ShopApply();
        try {
            result=shopApplyService.save(shopApply);
        }catch (FanZoneException e){
            log.error("【商家申请入驻】保存失败",e);
            map.put("error",e.getMessage());
            return new ModelAndView("buyer/join_us",map);
        }
        map.put("success","申请成功");
        return new ModelAndView("buyer/join_us",map);
    }

    @GetMapping("/load")
    public ModelAndView load(){
        return new ModelAndView("/buyer/join_us");
    }

 //   @PostMapping("/save")
//    public ResultVO<Map<String,Object>> frontSave(@Valid ShopApplyForm
//                                                          shopApplyForm,
//                                                  BindingResult bindingResult){
//        if(bindingResult.hasErrors()){
//            log.error("【商家申请入职】参数不正确,bussinessForm={}",shopApplyForm);
//            throw new FanZoneException(ResultEnum.PARAM_ERROR.getCode(),
//                    bindingResult.getFieldError().getDefaultMessage());
//        }
//
//        ShopApply shopApply= ShopForm2shopApply.convert(shopApplyForm);
//
//        //生成shopID，也是bussinessId
//        shopApply.setShopId(KeyUntil.getUniqueKey());
//        ShopApply result=new ShopApply();
//        try {
//            result=shopApplyService.save(shopApply);
//        }catch (FanZoneException e){
//            log.error("【商家申请入驻】保存失败}",e);
//        }
//        return ResultVOUntil.success(result);
//    }
}
